package com.cheeven.cwliu.citation.steps;

import java.util.HashMap;
import org.w3c.dom.Document;

public class Step9 extends StepBase {

	public static void clearMarkedList(HashMap<String, String> control) {
		logInfo("9. Return to Request CSV Page");

		logInfo(1, "returnUrl={0}", control.get("returnUrl"));
		String page = httpGet(control.get("returnUrl"));
		Document domdoc = toDOM(page);		
		String lastPageUrl = xpath("//td[@class='NEWbackLink']/a/@href", domdoc);
		
		logInfo(1, "lastPageUrl={0}", lastPageUrl);
		String lastPage = httpGet(lastPageUrl);
		domdoc = toDOM(lastPage);
		String deleteUrl = xpath("//input[@title='Remove these records from the Marked List']/@onclick", domdoc);
		deleteUrl = deleteUrl.substring(deleteUrl.indexOf("go_to_url('")+11);
		deleteUrl = "https://apps.webofknowledge.com/" + deleteUrl.substring(0, deleteUrl.indexOf("')"));
		
		/*
		 * Click "Clear Marked List"
		 */
		logInfo(1, "deleteUrl={0}", deleteUrl);
		String deletePage = httpGet(deleteUrl);
		domdoc = toDOM(deletePage);
		String exitPageUrl = xpath("//div[@class='NEWbackLink']/a/@href", domdoc);

		/*
		 * Click "<< Exit Marked List"
		 */
		logInfo(1, "exitPageUrl={0}", exitPageUrl);
		String exitPage = httpGet(exitPageUrl);
		domdoc = toDOM(exitPage);
		control.put("sid", xpath("//input[@name='SID']/@value", domdoc));
		control.put("qid", xpath("//input[@name='qid']/@value", domdoc));
		
		control.put("exitRefineSearchUrl", xpath("//div[@class='NEWbackLink']/a/@href", domdoc));
		logInfo(1, "exitRefineSearchUrl={0}", control.get("exitRefineSearchUrl"));

		control.put("logoutUrl", xpath("//a[.='Log Out']/@href", domdoc));
		logInfo(1, "logoutUrl={0}", control.get("logoutUrl"));
		
	}
}
